#include "cJSON.h"
#include <stdio.h>

int main(int argc, char const *argv[])
{
    // 字符串转对象 提取对应的值 
    char *json = "{\"name\":\"zhangsan\",\"age\":18,\"sex\":\"male\"}";
    cJSON *root= cJSON_Parse(json);
    if(root == NULL)
    {
        printf("json parse error\n");
        return -1;
    }
    cJSON *name = cJSON_GetObjectItem(root,"name");
    cJSON *age = cJSON_GetObjectItem(root,"age");
    printf("%s\n",name->valuestring);
    printf("%d\n",age->valueint);

    cJSON_Delete(root);

    //把值组合到一个json 字符串中
    cJSON *root2 = cJSON_CreateObject();
    cJSON_AddStringToObject(root2,"name","lisi");
    cJSON_AddNumberToObject(root2,"age",20);
    char *jsonStr=  cJSON_PrintUnformatted(root2);
    cJSON_Delete(root2);
    printf("%s\n",jsonStr);

    



    return 0;
}
